
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Constraints reference &#8212; Django 2.2.12.dev20200304094918 documentation</title>
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="Model _meta API" href="meta.html" />
    <link rel="prev" title="Model index reference" href="indexes.html" />



 
<script type="text/javascript" src="../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 2.2.12.dev20200304094918 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="indexes.html" title="Model index reference">previous</a>
     |
    <a href="../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="meta.html" title="Model &lt;code class=&#34;docutils literal notranslate&#34;&gt;&lt;span class=&#34;pre&#34;&gt;_meta&lt;/span&gt;&lt;/code&gt; API">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-models-constraints">
            
  <div class="section" id="s-module-django.db.models.constraints">
<span id="s-constraints-reference"></span><span id="module-django.db.models.constraints"></span><span id="constraints-reference"></span><h1>Constraints reference<a class="headerlink" href="#module-django.db.models.constraints" title="Permalink to this headline">¶</a></h1>
<div class="versionadded">
<span class="title">New in Django 2.2.</span> </div>
<p>The classes defined in this module create database constraints. They are added
in the model <a class="reference internal" href="options.html#django.db.models.Options.constraints" title="django.db.models.Options.constraints"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Meta.constraints</span></code></a>
option.</p>
<div class="admonition-referencing-built-in-constraints admonition">
<p class="first admonition-title">Referencing built-in constraints</p>
<p class="last">Constraints are defined in <code class="docutils literal notranslate"><span class="pre">django.db.models.constraints</span></code>, but for
convenience they’re imported into <a class="reference internal" href="../../topics/db/models.html#module-django.db.models" title="django.db.models"><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.db.models</span></code></a>. The standard
convention is to use <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">django.db</span> <span class="pre">import</span> <span class="pre">models</span></code> and refer to the
constraints as <code class="docutils literal notranslate"><span class="pre">models.&lt;Foo&gt;Constraint</span></code>.</p>
</div>
<div class="admonition-constraints-in-abstract-base-classes admonition">
<p class="first admonition-title">Constraints in abstract base classes</p>
<p class="last">You must always specify a unique name for the constraint. As such, you
cannot normally specify a constraint on an abstract base class, since the
<a class="reference internal" href="options.html#django.db.models.Options.constraints" title="django.db.models.Options.constraints"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Meta.constraints</span></code></a> option is
inherited by subclasses, with exactly the same values for the attributes
(including <code class="docutils literal notranslate"><span class="pre">name</span></code>) each time. Instead, specify the <code class="docutils literal notranslate"><span class="pre">constraints</span></code> option
on subclasses directly, providing a unique name for each constraint.</p>
</div>
<div class="admonition-validation-of-constraints admonition">
<p class="first admonition-title">Validation of Constraints</p>
<p class="last">In general constraints are <strong>not</strong> checked during <code class="docutils literal notranslate"><span class="pre">full_clean()</span></code>, and do
not raise <code class="docutils literal notranslate"><span class="pre">ValidationError</span></code>s. Rather you’ll get a database integrity
error on <code class="docutils literal notranslate"><span class="pre">save()</span></code>. <code class="docutils literal notranslate"><span class="pre">UniqueConstraint</span></code>s without a
<a class="reference internal" href="#django.db.models.UniqueConstraint.condition" title="django.db.models.UniqueConstraint.condition"><code class="xref py py-attr docutils literal notranslate"><span class="pre">condition</span></code></a> (i.e. non-partial unique constraints)
are different in this regard, in that they leverage the existing
<code class="docutils literal notranslate"><span class="pre">validate_unique()</span></code> logic, and thus enable two-stage validation. In
addition to <code class="docutils literal notranslate"><span class="pre">IntegrityError</span></code> on <code class="docutils literal notranslate"><span class="pre">save()</span></code>, <code class="docutils literal notranslate"><span class="pre">ValidationError</span></code> is also
raised during model validation when the <code class="docutils literal notranslate"><span class="pre">UniqueConstraint</span></code> is violated.</p>
</div>
<div class="section" id="s-checkconstraint">
<span id="checkconstraint"></span><h2><code class="docutils literal notranslate"><span class="pre">CheckConstraint</span></code><a class="headerlink" href="#checkconstraint" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="django.db.models.CheckConstraint">
<em class="property">class </em><code class="descname">CheckConstraint</code>(<em>*</em>, <em>check</em>, <em>name</em>)<a class="reference internal" href="../../_modules/django/db/models/constraints.html#CheckConstraint"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.CheckConstraint" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates a check constraint in the database.</p>
</dd></dl>

<div class="section" id="s-check">
<span id="check"></span><h3><code class="docutils literal notranslate"><span class="pre">check</span></code><a class="headerlink" href="#check" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.CheckConstraint.check">
<code class="descclassname">CheckConstraint.</code><code class="descname">check</code><a class="headerlink" href="#django.db.models.CheckConstraint.check" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A <a class="reference internal" href="querysets.html#django.db.models.Q" title="django.db.models.Q"><code class="xref py py-class docutils literal notranslate"><span class="pre">Q</span></code></a> object that specifies the check you want the constraint to
enforce.</p>
<p>For example, <code class="docutils literal notranslate"><span class="pre">CheckConstraint(check=Q(age__gte=18),</span> <span class="pre">name='age_gte_18')</span></code>
ensures the age field is never less than 18.</p>
</div>
<div class="section" id="s-name">
<span id="name"></span><h3><code class="docutils literal notranslate"><span class="pre">name</span></code><a class="headerlink" href="#name" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.CheckConstraint.name">
<code class="descclassname">CheckConstraint.</code><code class="descname">name</code><a class="headerlink" href="#django.db.models.CheckConstraint.name" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>The name of the constraint.</p>
</div>
</div>
<div class="section" id="s-uniqueconstraint">
<span id="uniqueconstraint"></span><h2><code class="docutils literal notranslate"><span class="pre">UniqueConstraint</span></code><a class="headerlink" href="#uniqueconstraint" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="django.db.models.UniqueConstraint">
<em class="property">class </em><code class="descname">UniqueConstraint</code>(<em>*</em>, <em>fields</em>, <em>name</em>, <em>condition=None</em>)<a class="reference internal" href="../../_modules/django/db/models/constraints.html#UniqueConstraint"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.db.models.UniqueConstraint" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates a unique constraint in the database.</p>
</dd></dl>

<div class="section" id="s-fields">
<span id="fields"></span><h3><code class="docutils literal notranslate"><span class="pre">fields</span></code><a class="headerlink" href="#fields" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.UniqueConstraint.fields">
<code class="descclassname">UniqueConstraint.</code><code class="descname">fields</code><a class="headerlink" href="#django.db.models.UniqueConstraint.fields" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A list of field names that specifies the unique set of columns you want the
constraint to enforce.</p>
<p>For example, <code class="docutils literal notranslate"><span class="pre">UniqueConstraint(fields=['room',</span> <span class="pre">'date'],</span>
<span class="pre">name='unique_booking')</span></code> ensures each room can only be booked once for each
date.</p>
</div>
<div class="section" id="s-id1">
<span id="id1"></span><h3><code class="docutils literal notranslate"><span class="pre">name</span></code><a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.UniqueConstraint.name">
<code class="descclassname">UniqueConstraint.</code><code class="descname">name</code><a class="headerlink" href="#django.db.models.UniqueConstraint.name" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>The name of the constraint.</p>
</div>
<div class="section" id="s-condition">
<span id="condition"></span><h3><code class="docutils literal notranslate"><span class="pre">condition</span></code><a class="headerlink" href="#condition" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.db.models.UniqueConstraint.condition">
<code class="descclassname">UniqueConstraint.</code><code class="descname">condition</code><a class="headerlink" href="#django.db.models.UniqueConstraint.condition" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>A <a class="reference internal" href="querysets.html#django.db.models.Q" title="django.db.models.Q"><code class="xref py py-class docutils literal notranslate"><span class="pre">Q</span></code></a> object that specifies the condition you want the constraint to
enforce.</p>
<p>For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">UniqueConstraint</span><span class="p">(</span><span class="n">fields</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;user&#39;</span><span class="p">],</span> <span class="n">condition</span><span class="o">=</span><span class="n">Q</span><span class="p">(</span><span class="n">status</span><span class="o">=</span><span class="s1">&#39;DRAFT&#39;</span><span class="p">),</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;unique_draft_user&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>ensures that each user only has one draft.</p>
<p>These conditions have the same database restrictions as
<a class="reference internal" href="indexes.html#django.db.models.Index.condition" title="django.db.models.Index.condition"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Index.condition</span></code></a>.</p>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Constraints reference</a><ul>
<li><a class="reference internal" href="#checkconstraint"><code class="docutils literal notranslate"><span class="pre">CheckConstraint</span></code></a><ul>
<li><a class="reference internal" href="#check"><code class="docutils literal notranslate"><span class="pre">check</span></code></a></li>
<li><a class="reference internal" href="#name"><code class="docutils literal notranslate"><span class="pre">name</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#uniqueconstraint"><code class="docutils literal notranslate"><span class="pre">UniqueConstraint</span></code></a><ul>
<li><a class="reference internal" href="#fields"><code class="docutils literal notranslate"><span class="pre">fields</span></code></a></li>
<li><a class="reference internal" href="#id1"><code class="docutils literal notranslate"><span class="pre">name</span></code></a></li>
<li><a class="reference internal" href="#condition"><code class="docutils literal notranslate"><span class="pre">condition</span></code></a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="indexes.html"
                        title="previous chapter">Model index reference</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="meta.html"
                        title="next chapter">Model <code class="docutils literal notranslate"><span class="pre">_meta</span></code> API</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/ref/models/constraints.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Mar 04, 2020</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="indexes.html" title="Model index reference">previous</a>
     |
    <a href="../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="meta.html" title="Model &lt;code class=&#34;docutils literal notranslate&#34;&gt;&lt;span class=&#34;pre&#34;&gt;_meta&lt;/span&gt;&lt;/code&gt; API">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>